package com.alifi.crm.web.home.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;

import com.alibaba.buc.sso.client.util.SimpleUserUtil;
import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.common.lang.StringUtil;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
import com.alibaba.platform.buc.sso.common.dto.SimpleSSOUser;
import com.alifi.crm.biz.guess.GuessMessage;
import com.alifi.crm.biz.lotteryzf.LotteryzfManager;
import com.alifi.crm.biz.lotteryzf.LotteryzfResult;
import com.alifi.crm.dal.object.WinRecordDO;

/**
 * 
 * 
 * @author wb_liangzhou.yan
 * @version $Id: LotteryAction.java, v 0.1 2013-4-24 下午07:03:40 wb_liangzhou.yan Exp $
 */
public class PayluckAction {
    
    @Resource(name = "lotteryzfManager")
    private LotteryzfManager lotteryzfManager;
    
    protected static final Logger      logger = LoggerFactory.getLogger(PayluckAction.class);
    
    protected static final Logger      logPermission = LoggerFactory.getLogger("uums-permission-logger");
    
    
    /**
     * @param request
     * @param response
     * @param ctx
     */
    public void doLottery(HttpServletRequest request, HttpServletResponse response,Navigator nav,final Context ctx){
        String status = "";
        String prizeType = "";
        try {
            ctx.put("action", "true");   //标记进入过action
            SimpleSSOUser user = SimpleUserUtil.findUser(request);
            if((user==null)||StringUtil.isBlank(user.getEmpId())){      //检查
                status = LotteryzfResult.不能抽奖.code();
                return;
            }
            ctx.put("user", user);
            
            //抽奖具体实现
            LotteryzfResult result = lotteryzfManager.lottery(user.getEmpId(),user.getNickNameCn(), user.getLastName());
            status = result.code();
            
            //if(status.equals(LotteryzfResult.已经抽奖过)) {
	            //获取个人中奖纪录
	            List<WinRecordDO> winRecords = lotteryzfManager.selectByEmployeeNum(user.getEmpId());
	            if(CollectionUtils.isNotEmpty(winRecords)){
	                if(StringUtil.isNotBlank(winRecords.get(0).getPrizeType())){
	                    prizeType = winRecords.get(0).getPrizeType();
	                }
	            }
            //}
        } catch (Exception e) {
            ctx.put("error", new GuessMessage("exception",e.getMessage(),""));
            logger.equals(e.fillInStackTrace());
        } finally{
            JSONObject json = new JSONObject();
            json.put("status", status);
            json.put("prizeType", prizeType);
            try {
                response.getWriter().println(json.toString());
            } catch (IOException e) {
                logger.error("IOException", e);
            }
        }
    }
    
}
